package com.kuang.lesson04;

import utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestTransaction01 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
//           关闭数据库的自动提交 自动会开启事务
            conn.setAutoCommit(false);

            String sql1 = "update account set money=money-100 where name= 'A'";
            st = conn.prepareStatement(sql1);
            st.executeUpdate();

//          int i=1/0;//报错

            String sql2="update account set money =money+100 where name =?";
            st= conn.prepareStatement(sql2);
           st.setString(1,"B");
            st.executeUpdate();

//            业务完毕，提交事务
            conn.commit();
            System.out.println("成功！");

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}